/*
 * test_encrypt.h
 *
 * Created: 7/2/2013 3:58:17 PM
 *  Author: Grigori
 */ 


#ifndef TEST_ENCRYPT_H_
#define TEST_ENCRYPT_H_

/////aaaaaaaaaaaaaaa start
#include "gpio.h"
#include "usart.h"
#include "print_funcs.h"
#include "stdio.h"
#include "ctaocrypt/test/test.h"
#include "cyassl/ctaocrypt/settings.h"
//#include <cyassl/openssl/ssl.h>
//#include "cyassl/test.h"
#include "cyassl/ctaocrypt/sha256.h"
//#include "ctaocrypt/test/test.h"

#include "cyassl/ssl.h"


#include "string.h"
void wait_for_a(void)
{
	int a = 0;
	while(1)
	{
		a = usart_getchar(DBG_USART);
		if(a == 'a')
		break;
	}
}
////aaaaaaaaaaaaaaa   end



void start_encrypt_test(void);

void start_encrypt_test(void)
{



//aaaaaaaaaaaaaaa   start
print_dbg("Press 'a' to continue....\r\n");

func_args server_args;

server_args.argc = 0x00000001;

wait_for_a();

ctaocrypt_test(&server_args);

//if (server_args.return_code != 0) return server_args.return_code;

print_dbg("All tests passed!\r\n");
//aaaaaaaaa  end





	
	/* Define a structure to hold the CyaSSL context. */
CYASSL_CTX* xCyaSSL_Context;

    /* Initialise CyaSSL.  This must be done before any other CyaSSL functions
    are called. */
    CyaSSL_Init();

    /* Attempt to create a context that uses the TLS V1 client protocol. */
    //xCyaSSL_Context = CyaSSL_CTX_new( CyaTLSv1_client_method() );

    //if( xCyaSSL_Context != NULL )
    //{
        ///* Load the CA certificate.  Real applications should ensure that
        //CyaSSL_CTX_load_verify_locations() returns SSL_SUCCESS before proceeding. */
        //CyaSSL_CTX_load_verify_locations( xCyaSSL_Context, "ca-cert.pem", 0 );
    //}
	
	
	
	
	
	
	
	
	
	CYASSL* xCyaSSL_Object;

    /* Standard Berkeley sockets connect function. */
    //if( connect( sockfd, (SA *) &servaddr, sizeof( servaddr ) ) == 0 )
    //{
        /* The connect was successful.  Create a CyaSSL object to associate with
        this connection.  The context created during initialisation is passed as
        the function parameter. */
        xCyaSSL_Object = CyaSSL_new( xCyaSSL_Context );
        
        //if( xCyaSSL_Object != NULL )
        //{
            ///* Associate the created CyaSSL object with the connected socket. */
            //CyaSSL_set_fd( xCyaSSL_Object, sockfd );
        //}
    //}
	
	
	
	
	
	
	
	
	
	int MAXLINE = 1000;
	
	char ucTxBuf[ MAXLINE ], ucRxBuf[ MAXLINE ];

	if( CyaSSL_write( xCyaSSL_Object, ucTxBuf, strlen( ucTxBuf ) ) != strlen( ucTxBuf ) )
	{
		/* Send failed. */
	}

	if( CyaSSL_read( xCyaSSL_Object, ucRxBuf, MAXLINE ) <= 0 )
	{
		/* Read failed. */
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/* CyaSSL objects should be deleted when they are no longer required. */
    CyaSSL_free( xCyaSSL_Object );
    
    /* The CyaSSL context should be deleted if it is no longer required.  However, 
    because most deeply embedded applications will keep the context for the lifetime 
    of the application, and only ever be restarted when the system is rebooted, it 
    might be that the context is never explicitly freed. */
    CyaSSL_CTX_free( xCyaSSL_Context );
    
    /* The library itself should be shut down cleanly if it too is no longer
    required.  Again, because most deeply embedded applications will require the
    library for the lifetime of the application, and only ever be restarted when 
    the system is rebooted, it might be that the library is never explicitly closed. */
    CyaSSL_Cleanup();

	
	
	
	
	
	
	
	
	












}

#endif /* TEST_ENCRYPT_H_ */